package org.LowAltitudeEconomy.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.LowAltitudeEconomy.pojo.Result;
import org.LowAltitudeEconomy.pojo.Sensor;
import org.LowAltitudeEconomy.service.SensorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.awt.*;

@RestController
@RequestMapping("/sensor")
public class SensorController {

    @Autowired
    private SensorService sensorService;

    //添加新的传感器(将point数据类型的坐标划分为经度和纬度)
    @PostMapping("/add")
    public Result<String> addSensor(@RequestParam String deviceSn, @RequestParam String longitude, @RequestParam String latitude, @RequestParam Integer userId) {
        if (!sensorService.existsByDeviceSn(deviceSn)) {
            sensorService.addSensor(deviceSn, longitude, latitude, userId);
            return Result.success("添加成功");
        }
        return Result.error("设备序列号已存在");
    }

    // 获取传感器的信息(传入参数的话，就根据参数查询；没传参数的话，就返回所有传感器的信息)
    @GetMapping("/all")
    public Result<Page<Sensor>> getAllSensor(
            @RequestParam(required = false) String deviceSn,
            @RequestParam(required = false) Integer userId) {

        if (deviceSn == null && userId == null) {
            Page<Sensor> page = sensorService.getAllSensors(new Page<>(1, 10));
            return Result.success(page);
        } else {
            Page<Sensor> page = sensorService.getSensorByDeviceSnAndUserId(deviceSn, userId, new Page<>(1, 10));
            return Result.success(page);
        }
    }


}




